﻿Public Class frmConsultaSala
    Dim objBBL As New BLLAgendamento
    Public ret As String = ""
    Public Pesquisa As String = ""
#Region "Atualização"
    Sub Atualiza()
        cbFiltro.SelectedIndex = 1
        txtLocalizar.Text = ""
        dgSala.DataSource = Nothing
    End Sub
    Sub CarregaGrid()
        Try
            dgSala.DataSource = objBBL.LocalizarSala(cbFiltro.SelectedIndex, txtLocalizar.Text)
            dgSala.Columns(0).HeaderText = "Código" ' define o nome da coluna
            dgSala.Columns(0).Width = 60 'define a largura da coluna
            dgSala.Columns(1).HeaderText = "Sala"
            dgSala.Columns(1).Width = 100
            dgSala.Columns(2).HeaderText = "Descrição"
            dgSala.Columns(2).Width = 245
            dgSala.Columns(3).HeaderText = "Bloco"
            dgSala.Columns(3).Width = 70
        Catch ex As Exception
        Finally
            txtLocalizar.Focus()
        End Try
    End Sub
#End Region
#Region "Validação"
    Private Sub cbFiltro_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbFiltro.KeyPress
        e.Handled = True
    End Sub
    Private Sub txtLocalizar_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLocalizar.KeyPress
        'Aqui Define o tipos de Caracteres que o TextBox Pode Aceitar
        'Se o ComboBox Estar no Indice 0(Código)
        If cbFiltro.SelectedIndex = 1 Then
            'Não deixo o Usuário Digitar Letras so Números
            'e Tambem libero para ele Poder Apagar o(s) Números
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            e.Handled = True
            bLocalizar_Click(sender, e)
        End If
    End Sub
    Private Sub cbFiltro_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txtLocalizar.Text = ""
    End Sub
#End Region
    Private Sub bLocalizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLocalizar.Click
        CarregaGrid()
    End Sub

    Private Sub tsbVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbVoltar.Click
        Pesquisa = ""
        Me.Close()
    End Sub


    Private Sub frmConsultaSala_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Atualiza()
        frmParticularidade.ret = "N"
        If Pesquisa <> "" Then
            txtLocalizar.Text = Pesquisa
            bLocalizar_Click(sender, e)
        End If

    End Sub

    Private Sub dgSala_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgSala.DoubleClick
        If ret = "S" Then
            Dim indice As Integer = -1
            Try
                For Each linha As DataGridViewRow In dgSala.SelectedRows
                    If (Not linha.Cells("sal_codigo") Is Nothing) Then
                        indice = linha.Index
                    End If
                Next
                If (indice <> -1) Then
                    frmParticularidade.codSala = dgSala.Rows(indice).Cells("sal_codigo").Value.ToString()
                    frmParticularidade.DesSala = dgSala.Rows(indice).Cells("sal_descricao").Value.ToString()
                    frmParticularidade.salSala = dgSala.Rows(indice).Cells("sal_sala").Value.ToString()
                    frmParticularidade.ret = "S"
                    Me.Close()
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
            End Try
        End If

    End Sub

    Private Sub dgSala_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgSala.CellDoubleClick
        Try

            If frmAgendamento.Tipo = "Retorno" Then
                Dim indice As Integer = -1
                'Percorre O DataGridView Verificando a Linha selecionada
                For Each linha As DataGridViewRow In dgSala.SelectedRows
                    If (Not linha.Cells("sal_codigo") Is Nothing) Then
                        'Guarda A Posição Selecionada
                        indice = linha.Index
                    End If
                Next
                If (indice <> -1) Then
                    frmAgendamento.CodSala = dgSala.Rows(indice).Cells("sal_codigo").Value.ToString()
                    frmAgendamento.DesSala = dgSala.Rows(indice).Cells("sal_descricao").Value.ToString()

                    Atualiza()
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub cbFiltro_SelectedIndexChanged_1(sender As System.Object, e As System.EventArgs) Handles cbFiltro.SelectedIndexChanged
        txtLocalizar.Text = ""
    End Sub
End Class